package com.smzd.core.actions;

import com.smzd.core.business.IService;
import com.smzd.core.domain.IntIdEntity;
import com.smzd.framework.utils.JsonHelper;

public abstract class AbstractDFController<E extends IntIdEntity> {

    protected abstract IService<E> getService();

    // 展示新建页面
    protected String toAddPage(E elem, String view) throws Exception {
        return getEditView(view);
    }

    // 保存数据
    protected String postAddData(E elem) throws Exception {
        getService().insert(elem);
        return JsonHelper.getSuccessJsonString("id", elem.getId());
    }

    // 展示查看列表页面
    protected String toListPage(String view) throws Exception {
        return getListView(view);
    }

    // // 给出列表数据
    // protected String getListData(RPage<E> page) throws Exception {
    // return getService().getListDataJson(page);
    // }

    // 展示编辑页面
    protected String toEditPage(String view) throws Exception {
        return getEditView(view);
    }

    // 保存更新数据
    protected String postEditData(E elem) throws Exception {
        getService().update(elem);
        return JsonHelper.getSuccessJsonString("id", elem.getId());
    }

    // 删除指定数据
    protected String postRemoveData(E elem) throws Exception {
        getService().remove(elem);
        return JsonHelper.getSuccessJsonString();
    }

    // 返回列表视图
    protected abstract String getListView(String view);

    protected abstract String getEditView(String view);
}
